import {
  format,
  startOfDay,
  endOfDay,
  subDays,
  startOfWeek,
  subWeeks,
  endOfWeek,
  startOfMonth,
  subMonths,
  endOfMonth,
  isSameDay,
} from 'date-fns'
import { zhCN } from 'date-fns/locale'

export const today = new Date()
export const timeStrChange = [
  {
    id: 0,
    name: '今天',
    dateRange: {
      startDate: startOfDay(today),
      endDate: endOfDay(today),
      key: 'selection',
    },
  },
  {
    id: 1,
    name: '昨天',
    dateRange: {
      startDate: startOfDay(subDays(today, 1)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
  {
    id: 2,
    name: '本周',
    dateRange: {
      startDate: startOfWeek(today, { weekStartsOn: 1 }),
      endDate: endOfDay(today),
      key: 'selection',
    },
  },
  {
    id: 3,
    name: '上周',
    dateRange: {
      startDate: startOfWeek(subWeeks(today, 1), { weekStartsOn: 1 }),
      endDate: endOfWeek(subWeeks(today, 1), { weekStartsOn: 1 }),
      key: 'selection',
    },
  },
  {
    id: 4,
    name: '本月',
    dateRange: {
      startDate: startOfMonth(today),
      endDate: endOfDay(today),
      key: 'selection',
    },
  },
  {
    id: 5,
    name: '上月',
    dateRange: {
      startDate: startOfMonth(subMonths(today, 1)),
      endDate: endOfMonth(subMonths(today, 1)),
      key: 'selection',
    },
  },
]

export const timeNumberChange = [
  {
    id: 11,
    name: '过去7天',
    dateRange: {
      startDate: startOfDay(subDays(today, 7)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
  {
    id: 12,
    name: '过去30天',
    dateRange: {
      startDate: startOfDay(subDays(today, 30)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
  {
    id: 13,
    name: '过去60天',
    dateRange: {
      startDate: startOfDay(subDays(today, 60)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
  {
    id: 14,
    name: '过去90天',
    dateRange: {
      startDate: startOfDay(subDays(today, 90)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
  {
    id: 15,
    name: '过去120天',
    dateRange: {
      startDate: startOfDay(subDays(today, 120)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
  {
    id: 16,
    name: '过去180天',
    dateRange: {
      startDate: startOfDay(subDays(today, 180)),
      endDate: endOfDay(subDays(today, 1)),
      key: 'selection',
    },
  },
]

export const formatDate = (date) => {
  if (!date) {
    return null
  }
  const startTimestamp = date.startDate.getTime()
  const endTimestamp = date.endDate !== null ? date.endDate.getTime() : null
  return {
    dates: [startTimestamp, endTimestamp],
    startStr: format(date.startDate, 'yyyy-MM-dd'),
    endStr: format(date.endDate, 'yyyy-MM-dd'),
  }
}

export const last7Days = () => {
  const initDate = timeNumberChange[0].dateRange
  return {
    startDate: format(initDate.startDate, 'yyyy-MM-dd'),
    endDate: format(initDate.endDate, 'yyyy-MM-dd'),
  }
}

export const locale = {
  ...zhCN,
  localize: {
    ...zhCN.localize,
    day: (n) => ['日', '一', '二', '三', '四', '五', '六'][n],
  },
}

export const isSameRangeDate = (date) => {
  if (!date) {
    // 没有存储则默认前七天
    return 11
  }
  for (let i = 0; i < timeStrChange.length; i++) {
    const val = timeStrChange[i]
    if (isSameDay(date.startDate, val.dateRange.startDate) && isSameDay(date.endDate, val.dateRange.endDate)) {
      return val.id
    }
  }
  for (let i = 0; i < timeNumberChange.length; i++) {
    const val = timeNumberChange[i]
    if (isSameDay(date.startDate, val.dateRange.startDate) && isSameDay(date.endDate, val.dateRange.endDate)) {
      return val.id
    }
  }

  return -1
}
